<?php
/**
 * CreateTime: 2023/6/21 15:29
 */

namespace App\Func;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Models\MeClient\MeClientModel;
use App\Models\MeLawCase\MeLawCase;
use App\Models\MeLawCase\MeLawCaseLawLetterModel;
use App\Models\MeLawCase\MeLawCasePhoneModel;
use App\Models\ShortLink\ShortLinkModel;
use App\Models\SMS\SMSLogModel;
use App\Models\SMS\SMSTemp;
use App\Scopes\MediationStaffScope;
use App\Tools\Sms\DbaicaiSms;

class SMSFunc
{
    public function replaceTemp($lawCaseInfo,$tempContent){
        if (strpos($tempContent, '{%respondent_name%}') !== false){
            $tempContent = str_replace("{%respondent_name%}", $lawCaseInfo['respondent_name'], $tempContent);
        }

        if (strpos($tempContent, '{%phone%}') !== false){
            $tempContent = str_replace("{%phone%}","028-61114211", $tempContent);
        }

        if (strpos($tempContent, '{%client_name%}') !== false){
            // 查询客户id
            $clientModel = new MeClientModel();
            $clientInfo = $clientModel->getByIdNotCompany($lawCaseInfo['client_id'],['client_name']);
            $tempContent = str_replace("{%client_name%}", $clientInfo['client_name'], $tempContent);
        }

        if (strpos($tempContent, '{%link_url%}') !== false){
            // 查询当前案件短链接code
            $latterModel = new MeLawCaseLawLetterModel();
            $latterInfo = $latterModel->getByLawCaseIdNotCom($lawCaseInfo['law_case_id']);
            if(isset($latterInfo['short_link_id'])){
                $shortInfo = (new ShortLinkModel())->withoutGlobalScope(MediationStaffScope::class)->where('short_link_id',$latterInfo['short_link_id'])->first();
                $tempContent = str_replace("{%link_url%}", 'https://t.cyhzsoft.com/'.$shortInfo['short_link_code'], $tempContent);
            }else{
                return false;
            }
        }
        $tempContent = str_replace(["\r","\n","\t"], "", $tempContent);
        return $tempContent;
    }

    public function latterSmsSend($lawCaseId,$tempId = 5,$phone = '15928410157'){
        $client = DbaicaiSms::getInstance();
        $client->changeRequestType('POST');
        // 生成短信配置data
        $lawCaseModel = new MeLawCase();
        $lawCasePhoneModel = new MeLawCasePhoneModel();
        $tempModel = new SMSTemp();
        // 查询模板
        $tempInfo = $tempModel->where('sms_temp_id',$tempId)->first();
        $tempStr = "";
        if($tempInfo&&isset($tempInfo['temp_content'])){
            $tempStr = $tempInfo['temp_content'] ;
        }
        $messageList = [];
        $smsLogList  = [];

        // 获取主叫号码
        $lawCaseInfo = $lawCaseModel->getById($lawCaseId,['law_case_id','respondent_name','client_id','main_respondent_phone_id']);
        $tempContent = $this->replaceTemp($lawCaseInfo,$tempStr);
        if(!$tempContent){
            return 1;
        }
        $phoneNumber = $lawCasePhoneModel->select(['phone_number'])->where('law_case_phone_id',$lawCaseInfo['main_respondent_phone_id'])->first();
        if(!$phoneNumber||!isset($phoneNumber['phone_number'])){
            return 2;
        }
        // 获取主叫号码
        $messageList[] = [
            'phone' => $phone,
            'content' => $tempContent
        ];
        $smsLogList = [
            'phone' => $phone,
            'sms_info' => $tempContent,
            'law_case_id' => $lawCaseId,
            'temp_sms_id' => $tempId
        ];
        $smsLogModel = new SMSLogModel();
        $smsLogModel->add($smsLogList);
        $data = $client->sendMsg($messageList);
        $res = $client->execute('api/sendMessageOne',$data);
        $fmtRes = json_decode($res,true);
        if($fmtRes['code']){
            return false;
        }
        return $fmtRes;
    }
}